package org.myh.excel.example.builder;



import org.myh.excel.ConcurrentExcelBufferBuilder;
import org.myh.excel.ExcelDataCallback;
import org.myh.excel.FieldSort;
import org.myh.excel.concurrent.MICClassDivisibleExcelBuffer;
import org.myh.excel.concurrent.SimpleExcelBuffer;
import org.myh.excel.example.ExcelDto;
import org.myh.excel.pojo.BuildDataSource;
import org.myh.excel.support.CRECallBackDataWithDBConnect;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * ClassName: BuildMultithreadReadToCallback
 * Description:
 * date: 2024/3/26 14:26
 *
 * @author Yu Han
 * @since JDK 1.8
 */
public class BuildMultiThreadReadToCallback implements ExcelDataCallback<FieldSort[]> {


    public void demo() throws Exception {
        //1. 基于jdbc的，会触发getConnectAndExcelData(Connection connection, HashMap<Integer, List<T>> data)回调
        Path path = Paths.get("C:\\Users\\Administrator\\Desktop", "宿舍信息上传模板.xlsx");
        InputStream inputStream2 = Files.newInputStream(path);
        Properties propertier = new Properties();
        propertier.setProperty("user","root");
        propertier.setProperty("password","123456");
        propertier.setProperty("url","jdbc:mysql://localhost:3306/smart_oam?useAffectedRows=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true");

        // 1.1 需要传入Driver 与 Properties，默认不选择的话是mybatis类型，通过 public void getConnectAndExcelData(HashMap<Integer, List<T>> data) 回调
        //基于mysql
//        com.mysql.jdbc.cj.Driver driver = com.mysql.jdbc.cj.Driver.new Driver();
        CRECallBackDataWithDBConnect dataWithDBConnect = ConcurrentExcelBufferBuilder
                .instance()
                .useType(CRECallBackDataWithDBConnect.JDBC_TYPE)
                .addDataSource(new BuildDataSource(null, propertier))
                .build();

        Long l1 = System.currentTimeMillis();
        System.out.println("1开始时间"+l1);
        dataWithDBConnect.setDivisibleExcelBuffer(new MICClassDivisibleExcelBuffer(inputStream2, 2048, 500,0, ExcelDto.class));
        dataWithDBConnect.exec(this);
        System.out.println("1结束时间"+( System.currentTimeMillis() - l1));


        // 2 CRECallBackDataWithDBConnect 对象可以重复使用，只需要传入数据源即可
        // 2.1 采用匿名对象回调
        InputStream inputStream1 = Files.newInputStream(path);
        //这里使用SimpleExcelBuffer返回String[]
        dataWithDBConnect.setDivisibleExcelBuffer(new SimpleExcelBuffer(inputStream1,2048, 500,0));
        ExcelDataCallback<String[]> excelDataCallback = new ExcelDataCallback<String[]>() {
            @Override
            public void getConnectAndExcelData(Connection connection, HashMap<Integer, List<String[]>> data) {
                //这个是基于传统jdbc的回调，会自动为调用者创建jdbc连接
                try {
                    connection.setAutoCommit(false);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            @Override
            public void getConnectAndExcelData(HashMap<Integer, List<String[]>> data) {
                //这个是基于其他方式，如mybatis

            }
        };
        dataWithDBConnect.exec(excelDataCallback);

        //选择sheet(实际上一个inputStream只能读取一次，需要再次传入流，因本版本为做流复用）
        dataWithDBConnect.cutSheet(1);
        dataWithDBConnect.exec(excelDataCallback);
    }


    @Override
    public void getConnectAndExcelData(Connection connection, HashMap<Integer, List<FieldSort[]>> data) {
        System.out.println("本体接收到回调消息");
    }

    @Override
    public void getConnectAndExcelData(HashMap<Integer, List<FieldSort[]>> data) {

    }

    public static void main(String[] args) throws Exception {
        BuildMultiThreadReadToCallback callback = new BuildMultiThreadReadToCallback();
        callback.demo();
    }
}
